﻿using System;
using System.Collections.Generic;
using Entity;
using Utils;
using System.Linq;
using Entity.Dto;
using DataBaseConnect.IRepositories;

namespace DataBaseConnect.App
{
    public class MessageAppService : IMessageAppService
    {

        public IMessageRepository _messageMqRepository;
        public IUserAppService _userAppService;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="userRepository">仓储对象</param>
        public MessageAppService(IMessageRepository messageMqRepository, IUserAppService userAppService)
        {
            _messageMqRepository = messageMqRepository;
            _userAppService = userAppService;
        }

        /// <summary>
        /// 获取消息(分页)
        /// <param name="MenuName">菜单名</param>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页显示多少条</param>
        /// <param name="rowCount">总条数</param>
        /// <returns></returns>
        public List<MessageDto> GetMessageList(int IsSee, int page, int limit, out int rowCount)
        {
            var message = (from m in _messageMqRepository.LoadPageListOne(page,limit, out rowCount, f => f.IsSee == IsSee,f=>f.SendTime).ToList()
                           join u in _userAppService.GetUserList("")
                           on m.SendUserId equals u.UserId
                           select new MessageDto
                           {
                               Content = m.Content,
                               IsSee = m.IsSee,
                               Id = m.Id,
                               ReadTime = m.ReadTime,
                               SendTime = m.SendTime,
                               SendUserName = u.UserName
                           }).ToList();

            return message;
        }
        /// <summary>
        /// 获取消息条数
        /// </summary>
        /// <param name="IsSee"></param>
        /// <returns></returns>
        public List<MessageDto> GetMessageCount(int IsSee)
        {
            var message = (from m in _messageMqRepository.GetAllList(f => f.IsSee == IsSee).ToList()
                           join u in _userAppService.GetUserList("")
                           on m.SendUserId equals u.UserId
                           select new MessageDto
                           {
                               Content = m.Content,
                               IsSee = m.IsSee,
                               Id = m.Id,
                               ReadTime = m.ReadTime,
                               SendTime = m.SendTime,
                               SendUserName = u.UserName
                           }).ToList();

            return message;
        }

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="input">信息实体</param>
        /// <returns></returns>
        public ReceiveStatus AddMessage(Message input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _messageMqRepository.Insert(input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败:" + ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 标记为已读
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ReceiveStatus UpdateMessage(int Id)
        {
            return _messageMqRepository.UpdateMessage(Id);
        }

       
    }
}
